package com.bjfu.demo.utils;

import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {

    private Integer code;
    private String message;
    private T data;

    private Result() {
    }

    public Result(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }

    // 后端使用 通用正确
    public static <T> Result<T> ok() {
        return new Result<>(ResultCode.OK);
    }

    public static <T> Result<T> ok(String message) {
        Result<T> Result = new Result<>();
        Result.code = 200;
        Result.message = message;
        return Result;
    }

    public static <T> Result<T> ok(String message, T data) {
        Result<T> ok = Result.ok(message);
        ok.data = data;
        return ok;
    }

    // 后端使用通用错误
    public static <T> Result<T> fail() {
        return new Result<>(ResultCode.ERROR);
    }

    public static <T> Result<T> fail(String message) {
        Result<T> Result = new Result<>();
        Result.code = 500;
        Result.message = message;
        return Result;
    }

    public static <T> Result<T> fail(String message, T data) {
        Result<T> fail = Result.fail(message);
        fail.data = data;
        return fail;
    }

    public static <T> Result<T> fail(Integer code, String message) {
        Result<T> Result = new Result<>();
        Result.code = code;
        Result.message = message;
        return Result;
    }

    public static <T> Result<T> fail(ResultCode resultCode) {
        return new Result<>(resultCode);
    }


    public Result<T> data(T data) {
        this.data = data;
        return this;
    }
}
